package com.forestar.mapControl.utils.tile;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLSocketFactory;

import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.OkUrlFactory;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 缺少okhttp的库
 */
public class MzTileNetworkUtils
  {
    private static OkHttpClient DEFAULT_CLIENT = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30,TimeUnit.SECONDS)
            .connectionPool(new ConnectionPool(60,60,TimeUnit.SECONDS))
            .build();
    private static OkUrlFactory DEFAULT_URL_FACTORY = new OkUrlFactory(DEFAULT_CLIENT);
    private static String USER_AGENT = MapboxUtils.getUserAgent();

    public static boolean isNetworkAvailable(Context context)
      {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
      }

    public static HttpURLConnection getHttpURLConnection(final URL url)
      {
        return getHttpURLConnection(url, null, null);
      }

    public static HttpURLConnection getHttpURLConnection(final URL url, final okhttp3.Cache cache)
      {
        return getHttpURLConnection(url, cache, null);
      }

    public static HttpURLConnection getHttpURLConnection(final URL url, final okhttp3.Cache cache, final SSLSocketFactory sslSocketFactory)
      {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(10, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS);
        if (cache != null)
          {
            builder.cache(cache);
          }
        if (sslSocketFactory != null)
          {
            builder.sslSocketFactory(sslSocketFactory);
          }
        OkHttpClient client = builder.build();
        HttpURLConnection connection = new OkUrlFactory(client).open(url);
        connection.setRequestProperty("User-Agent", MapboxUtils.getUserAgent());
        return connection;
      }

    public static HttpURLConnection getURLConnection(final URL url)
      {
        HttpURLConnection connection = DEFAULT_URL_FACTORY.open(url);
        connection.setRequestProperty("User-Agent", USER_AGENT);
        return connection;
      }

//    public static Cache getCache(final File cacheDir, final int maxSize) throws IOException
//      {
//        return new Cache(cacheDir, maxSize);
//      }

    //by:petra
    //    获取一张瓦片的字节数组。
    //    临时函数，下一步整个类都需要整理，然后重构相关下载。
    public static byte[] getTileStream(URL url) throws IOException
      {
        //网上拉取瓦片的请求
        Request request = new Request.Builder().url(url)
                .header("Keep-Alive","300")
            .addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36")
            .build();
        Response response = DEFAULT_CLIENT.newCall(request).execute();
        if (response == null)
          {
            return null;
          }
        byte[] bytes = response.body().bytes();
        response.close();
        return bytes;
      }
  }
